using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using Damany.Cameras;
using Damany.Imaging.Common;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using FaceSearchWrapper;


namespace WindowsApplication1
{
    public partial class Form1 : XtraForm
    {
        private AForge.Video.IVideoSource _videoSource;
        private FaceSearch _faceSearch;
        private int _faceCount;
        private int _totalCount;
        private Image _image;


        public Form1()
        {
            InitializeComponent();
        }

        private void InitCamera()
        {
            //var videoSource =new DirectoryFilesCamera((string)this.cameraUri.EditValue, "*.jpg");
            var endpoint = new IPEndPoint(IPAddress.Parse(cameraUri.Text), 8888);
            var videoSource = new CyNetCamera(endpoint);
            videoSource.NewFrame += VideoSourceOnNewFrame;
            videoSource.Start();
        }

        private void VideoSourceOnNewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            var img = eventArgs.Frame;
            Action<Bitmap> showPic = i => this.pictureEdit1.Image = i;
            this.BeginInvoke(showPic, img);

        }

        private void InitPortraitFinder()
        {
            _faceSearch = new FaceSearch();
            _videoSource.NewFrame += _videoSource_NewFrame;
        }

        void _videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            _image = new Bitmap(eventArgs.Frame);
            var ipl = OpenCvSharp.IplImage.FromBitmap(AForge.Imaging.Image.Clone(eventArgs.Frame));
            var faces = _faceSearch.SearchFace(ipl);
            DrawFacesRect(faces);
            _faceCount = faces.Length;
            _totalCount += _faceCount;

            UpdateFacecountLabel();
        }

        private void DrawFacesRect(PortraitBounds[] faces)
        {
            using (var g = Graphics.FromImage(_image))
            using (var pen = new Pen(Color.Red, 3))
            {
                foreach (var p in faces)
                {
                    g.DrawRectangle(pen, p.Bounds);
                }
            }
        }

        private void UpdateFacecountLabel()
        {
            if (InvokeRequired)
            {
                this.BeginInvoke((Action)UpdateFacecountLabel);
                return;
            }

            this.pictureEdit1.Image = this._image;
            this.faceCountLabel.Text = _faceCount.ToString();
            this.totalCountLabel.Text = _totalCount.ToString();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            InitCamera();
            //InitPortraitFinder();
        }

        private void faceCountLabel_Click(object sender, EventArgs e)
        {

        }


    }
}